<?php
//
// TextField
//
class TextField {
  public $label;
  public $name;
  public $value = "";
  public $html = "";

  function render() {
    echo "<label for='$this->name'> $this->label </label>
          <input type='text' id='$this->name' 
                 name='$this->name' 
                 value='$this->value' $this->html />";
  
  }
}
 

//
// TextArea
//
class TextArea {
  public $label;
  public $name;
  public $value = "";
  public $html = "";

  function render() {
    echo "<label for='$this->name'> $this->label </label>
          <textarea id='$this->name' 
                 name='$this->name' 
                 $this->html>$this->value</textarea>";
  
  }
}


//
// PasswordField
//
class PasswordField {
  public $label;
  public $name;
  public $value = "";
  public $html = "";

  function render() {
    echo "<label for='$this->name'> $this->label </label>
          <input type='password' id='$this->name' 
                 name='$this->name' 
                 value='$this->value' $this->html />";
  
  }
}


//
// SimpleSelect
//
class SimpleSelect {
  public $label = "";
  public $name = "";
  public $options = array("", "");
  public $selected = "";
  public $blank = true;
  public $html = "";

  function render() {
    echo "<label for='$this->name'> $this->label </label>";
    echo "<select id='$this->name' 
                  name='$this->name' 
                  $this->html >";

    // If $blank, we add a blank option
    if ($this->blank == true) echo '<option value=""></option>';

    // We iterate over $options to generate the <option> tags.
    foreach(array_keys($this->options) as $option_description) {
      // If the value of the option is leaved blank
      // We use the description as the value
      if (empty($this->options[$option_description]) || 
        $this->options[$option_description] == null) {
        $option_value = $option_description;
      } else {
        $option_value = $this->options[$option_description];
      }


      // Here we check if the $option_value is the selected one.
      if ($option_value == $this->selected) {
        echo "<option value='$option_value' selected> 
                $option_description 
              </option>";
      } else {
        echo "<option value='$option_value'> 
                $option_description 
              </option>";
      }
    } // End foreach $options


    // End the select tag
    echo "</select>";   
  }
}


//
// MultipleSelect
//
class MultipleSelect {
  public $label;
  public $name;
  public $options;
  public $selected = array();
  public $blank = true;
  public $html = "";

  private $name_without_brackets;

  function render() {
    // Fix the this-selected property, should be an array
    if (is_string($this->selected)) {
      $this->selected = split(",", $this->selected);
    }

    // Remove brackets from the name, to make a string to be placed
    // on the id attribute.
    $this->name_without_brackets = str_replace("[]", "", $this->name);

    // We check if the $name ends with []
    // This is necessary to PHP handle the multiple select
    if (stripos($this->name, "[]") == false) {
      echo "<br/> 
            <b> Warning: </b> <i>$this->name</i> multiple select:
            The name should end in '[]' to PHP handle the multiple. 
            <br/>";
    }

    echo "<label for='$this->name'> $this->label </label>";
    echo "<select multiple id='$this->name_without_brackets' 
                  name='$this->name' $this->html>";


    // If $blank, we add a blank option
    if ($this->blank == true) echo '<option value=""></option>';

    // We iterate over $options to generate the <option> tags.
    foreach(array_keys($this->options) as $option_description) {
      // If the value of the option is leaved blank
      // We use the description as the value
      if (empty($this->options[$option_description]) || 
          $this->options[$option_description] == null) {
        $option_value = $option_description;
      } else {
        $option_value = $this->options[$option_description];
      }

 
      // Here we check if the $option_value is the selected one.
      if (in_array($option_value, $this->selected)) {
        echo "<option value='$option_value' selected> 
                $option_description 
              </option>";
      } else {
        echo "<option value='$option_value'> 
                $option_description 
              </option>";
      }
    } // End foreach $options


    // End the select tag
    echo "</select>";   
  } // render()
}


?>

